package jp.co.hcs.attendancemanagement.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import jp.co.hcs.attendancemanagement.AMConstants;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.LookupDispatchAction;

/**
 * The Class AMLookupDispatchAction.
 */
public abstract class AMLookupDispatchAction extends LookupDispatchAction {

  /** The Constant LOGIN. */
  protected static final String LOGIN = "login";

  /** The Constant SUCCESS. */
  protected static final String SUCCESS = "success";

  /** The Constant INVALID_TOKEN. */
  protected static final String INVALID_TOKEN = "invalid_token";

  /** The Constant INVALID_SESSION. */
  protected static final String INVALID_SESSION = "invalidSession";

  public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
      HttpServletResponse response) throws Exception {

    HttpSession session = request.getSession(false);
    if (session == null || session.isNew()) {
      return mapping.findForward(INVALID_SESSION);
    }

    if (request.getSession().getAttribute(AMConstants.USER_LOGGED_IN) != null) {
      String method = request.getParameter("method");
      if (method != null) {
        ActionForward forward = super.execute(mapping, form, request, response);
        return forward;
      } else {
        return mapping.findForward(INVALID_SESSION);
      }
    }
    return mapping.findForward(LOGIN);
  }

}
